﻿var dbnr=$kj("dbnr");
var bbv=$kj("bbv");
var xsyc=$kj("xsyc");
var xul=$kj("dbnr","ul")[0];
var dbimg=$kj("dbimg");



//初始化页面选框
function renew()
{
	var cooks=GetCookie("listhc").split("/");
	var bdbb=$kj("bdbb","input");
	for(var c=1;c<cooks.length;c++){var wf=cooks[c].split("_");for(var i=0;i<bdbb.length;i++){if(bdbb[i].id=="a"+wf[0]){bdbb[i].checked=true;break;}}}
}

//初始化漂浮层
function newpos()
{
	var ouli="",alls=0;
	var cooks=GetCookie("listhc").split("/");
	for(var i=1;i<cooks.length;i++){
		alls++;
		var wf = cooks[i].split("_");
        var img  = jQuery('#products_img_'+wf[0]).attr('src');
        if(img!=undefined) {
            ouli += "<li class='prelative'><span class='closs2' title='点击关闭' onclick='delli(" + wf[0] + ")'></span><img src='" + img + "' align='absmiddle'/></li>";
        }
	}
	xul.innerHTML=ouli;
	//dbimg.innerHTML=alls;s
	$kj("tishi").style.display=(ouli=="")?"block":"none";
	xul.style.display=(ouli=="")?"none":"block";
	jQuery('.db_border ul li').on('mouseover',function(){
			jQuery(this).children('.closs2').show();
		}).on('mouseout',function(){
			jQuery(this).children('.closs2').hide();
			});
	
	if( cooks.length >1  && jQuery(dbimg).css('display') == 'none')
	{
		jQuery(dbimg).css('display','block')
	}
	
	if( typeof wf != 'undefined' )
	{
		jQuery( '#fxid_' + wf[0] ).addClass('selectshare'); //当前已选分享按钮
	}
	
	
	//if(!alls){dbnr.style.display="none";}
}

//点击处理，增加使用其他按钮加入分享的功能-liuliqiang，090304增加替换/分割符
function dblist(id,event,linkproname)
{
    jQuery("section.conRight").hide();
	if(typeof(linkproname) != "undefined" && linkproname.length > 0)
    {
	    /*linkproname如果为图片地址，替换"/"为其他符号，避免cookie分割产品冲突  *kj编辑*/
	    linkproname = linkproname.replace("/","|");
		
		var nrid = $kj("link"+id);
	    var cook = GetCookie("listhc");
	    var cooks = cook.split("/");
        /*判断是否重复添加*/
        if(cook.indexOf("/"+id) > -1)
        {
            alert(linkproname+"已经加入分享");
            return;
        }
        if(cooks.length>10)
		{
			alert("最多只允许添加10条");
			return;
		}
		else
		{
			SetCookie("listhc",cook+"/"+id+"_"+linkproname.replace("/","&%#"));
			zbyd(window.event||event);
		}
	   
    }
    else
    {
	    var cook=GetCookie("listhc");
	    var cooks=cook.split("/");
        //090305liuliqaing修改：支持页面上有多个相同产品
        var ischecked = false;
        var nrid=document.getElementsByTagName("input");
        for(var i = 0;i<nrid.length;i++)
        {
            if(nrid[i].id == "a"+id && nrid[i].checked)
            {
                //增加判断重复-liuliqiang
                if(cook.indexOf("/"+id) > -1)
                {
                    alert(nrid[i].value+"已经加入分享");
                    return;
                }
                else if(cooks.length>10)
                {
                    nrid[i].checked = false;alert("最多只允许添加10条");
					jQuery('#fxid_'+id).removeClass('selectshare'); //还原点击分享按钮
					return;
                }
                else
                {
                    SetCookie("listhc",cook+"/"+id+"_"+nrid[i].value.replace("/","&%#"));ischecked=true;zbyd(window.event||event);
                }
            }
        }
        if(!ischecked)
	        delCookie(id);
	}
	
	newpos();
	dbnr.style.display="block";
}
//漂浮窗口删除
function delli(id){
    delCookie(id);
    var nrid=document.getElementsByTagName("input");
    for(var i = 0;i<nrid.length;i++)
    {
        if(nrid[i].id == "a"+id)
        {
            nrid[i].checked = false;
        }
    }
	
	jQuery('#fxid_'+id).removeClass('selectshare'); //还原点击分享按钮
    newpos();
}

//设置COOKIE  //liuliqiang 增加设置path，使各目录能取到相同的COOKIE
function SetCookie(name,value)
{
	var exp=new Date();
	exp.setTime(exp.getTime()+24*3600000);
	document.cookie = name+"="+escape(value)+";expires="+exp.toGMTString()+";path=/";
}

//获得COOKIE
function GetCookie(name)
{
	var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr!=null){
		
		return unescape(arr[2]);
	}
	else
	{
		return "";
	}
}

//删除COOKIE
function delCookie(id)
{
	var cooks=GetCookie("listhc").split("/"),dstr="";
	for(var i=1;i<cooks.length;i++){
		if(cooks[i].split("_")[0]!=id)
		{
			dstr+="/"+cooks[i];
		}
	}
	SetCookie("listhc",dstr);
}

function clears()
{
	SetCookie("listhc","")
	newpos();
	var kk=$kj("bdbb","input").length;
	for(var k2=0;k2<kk;k2++)
	{
		$kj("bdbb","input")[k2].checked=false
	}
}

//增加点击开始分享的处理--liuliqiang
function comparepro()
{
    if(!islogin)
    {
        dologin();
    }else {
        var cook = GetCookie("listhc");
        var cooks=cook.split("/"), csrfToken = $("#_csrf").val() || $('meta[name="csrf-token"]').attr("content");
        var url =  "pid=";//"_csrf=" + csrfToken +
        for (var i = 0; i < cooks.length; i++) {
            var proid = cooks[i].substr(0, cooks[i].indexOf("_"));
            if (proid.length > 0)
                url += proid + ",";
        }
        if (url.length > 7) {            
            location.href = '/products/listsharer?'+url;
        }
        else {
            alert("请先选择要分享的产品");
        }
        return false;
    }
}

//提示位置方法
function zbyd(event)
{
	var sw=50,sh=50,vw=15,vh=15,tjs=35;
	var sl=bbv.l();
	var vl=(event.pageX||(event.clientX+(document.documentElement.scrollLeft||document.body.scrollLeft)))-7;
	var st=(document.documentElement.scrollTop||document.body.scrollTop)+300;
	var vt=(event.pageY||(event.clientY+(document.documentElement.scrollTop||document.body.scrollTop)))-7;
	var spl=Math.floor(Math.abs((sl-vl)/tjs)),spt=Math.abs((st-vt)/tjs);
	xsyc.style.display="block";xsyc.l(vl);xsyc.t(vt);xsyc.w(vw);xsyc.h(vh);
	var maxTime=setInterval(function()
	{
		xsyc.l((vl+spl)<sl?vl+=spl:((vl-spl)>sl?vl-=spl:vl=sl));xsyc.t((vt+spt)<st?vt+=spt:((vt-spt)>st?vt-=spt:vt=st));
		xsyc.w(vw+2<sw?vw++:vw=sw);xsyc.h(vh+2<sh?vh++:vh=sh);
		tjs--;if(!tjs){xsyc.style.display="none";clearInterval(maxTime);}
	},10);
}

//显示/隐藏列表
function dbhidd()
{
	$dbnr_ = jQuery(dbnr);
	$dbimg_ = jQuery(dbimg);
	if( $dbimg_.css('display')=='block' && $dbnr_.css('display') == 'none' )
	{
		$dbnr_.css('display','block');
		$dbimg_.css('display','none');
        jQuery("section.conRight").hide();
	}else if( $dbimg_.css('display')=='none' && $dbnr_.css('display') == 'block' )
	{
		$dbnr_.css('display','none');
		$dbimg_.css('display','block');
        jQuery("section.conRight").show();
	}
	else
	{
		$dbnr_.css('display','none');
		$dbimg_.css('display','block');
        jQuery("section.conRight").show();
	}
	
	
	//dbimg.style.display = (dbimg.style.display=="block")?"none":"block";
	//dbnr.style.display = (dbnr.style.display=="block")?"none":"block";
	
} //none

newpos();
renew();


//功能重置需要 2016.8.16
function resetShar()
{
	dbnr=$kj("dbnr");
	bbv=$kj("bbv");
	xsyc=$kj("xsyc");
	xul=$kj("dbnr","ul")[0];
	dbimg=$kj("dbimg");
	
	newpos();
	renew();
}